﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SetCourseLayoutRole.aspx.cs" Inherits="ZX.WebApp.Crm.MeetingArrange.SetCourseLayoutRole" %>
<%@ Register Assembly="ZX.Web.UI.Controls" Namespace="ZX.Web.UI.Controls" TagPrefix="cc1" %>
<%@ Register Assembly="ZX.Web.UI.Controls" Namespace="ZX.Web.UI.Controls.UserControl"
    TagPrefix="cc2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/reset-fonts-grids/reset-fonts-grids.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/container/assets/skins/sam/container.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/menu/assets/skins/sam/menu.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/button/assets/skins/sam/button.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/layout/assets/skins/sam/layout.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/resize/assets/skins/sam/resize.css" />
    <link rel="stylesheet" type="text/css" href="../../Scripts/yui_2.9.0/build/datatable/assets/skins/sam/datatable.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/treeview/assets/skins/sam/treeview.css" />
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/element/element-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/datasource/datasource-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/get/get-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/dragdrop/dragdrop.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/calendar/calendar-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/datatable/datatable-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/layout/layout-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/button/button-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/container/container-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/treeview/treeview-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/resize/resize-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/paginator/paginator-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/event-delegate/event-delegate-min.js"></script>
    <script src="../../Scripts/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../../Scripts/Jquery.form.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.json-2.2.js"></script>
    <style type="text/css">
    .yui-skin-sam table {
	    width: 100%;
    }
    .selectedCss
    {
        background-color:Red;   
    }
    .unSelectedCss
    {
        background-color:Blue;
     }
    </style>
</head>
<body class="yui-skin-sam">
    <form id="form1" runat="server">
    <div>
        <div id="fieldLeft">
           <div style="margin:2px;">
                <p>
                    <cc2:DeptList ID="DeptList1" runat="server"></cc2:DeptList>
                    <input type="text" id="txtSearch" style="width:143px; height:20px;" />
                    <input type="button" id="btnSearch" value="查询" onclick="return SearchName();" style="width:50px; text-align:center;" />
                </p>
                <div id="divUser" style="margin-top:10px;">
                     <%--<span><input type="checkbox" /><a href="javascript:void(0)" class='aSelectUser'>人员1</a></span><br />
                     <span><input type="checkbox" /><a href="javascript:void(0)" class='aSelectUser'>人员2</a></span><br />--%>
                </div>
           </div>
        </div>
        <div id="fieldRight">
            <cc1:SelfDefineTree ID="TreeView1" runat="server" AllowCheck="true" TreeType="productlayout"></cc1:SelfDefineTree>
        </div>
        <div id="toolbar" style="margin-top: 12px; text-align: center;">
            <button type="button" id="btnSave" name="btnSave">
                保存</button>&nbsp;&nbsp;
            <button type="button" id="btnClose" name="btnClose">
                取消</button>
        </div>
    </div>
    <div id="testDIV" style="display:none;"></div>
    </form>
    <form id="form2" style="display: none" action="">
    <textarea style="display: none" id="jsonDataContent" rows="2" cols="2" name="jsonDataContent"></textarea>
    </form>
</body>

<script type="text/javascript">
    var btnSave = "", btnClose = "";
    (function () {
        var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;
        Event.onDOMReady(function () {
            var layout = new YAHOO.widget.Layout({
                units: [
                    { position: 'left', body: 'fieldLeft', gutter: '0 1 1 1',
                        width:210, minWidth: 170, scroll: true, animate: true
                    },
                    { position: 'bottom', height: 60, body: 'toolbar', gutter: '1 1 1 1', scroll: null, zIndex: 2 },
                    { position: 'center', body: 'fieldRight', gutter: '1 2 1 1', scroll: true}]
            });
            layout.render();
            bindSelectChange();
        });
        var makeBranch = function (parent, label) {
        }
        tree2 = new YAHOO.widget.TreeView("TreeView1");
        makeBranch(tree2.getRoot());
        tree2.setNodesProperty('propagateHighlightUp', true);
        tree2.setNodesProperty('propagateHighlightDown', true);
        tree2.subscribe('clickEvent', tree2.onEventToggleHighlight);
        //tree2._nodes[1].expand();
        tree2.expandAll();  
        tree2.render();

        LoadDatas("d17eb523-1c10-49c9-a111-073c2c020bd7"); //初始加载会务部
    })();
    //改变节点处理函数
    function Native(caption, key) {
        fkey = key;
    }
    YAHOO.example.init = function () {
        function onButtonClick(p_oEvent) {
            switch (this.get("id")) {
                case "btnSave":
                    Save();
                    break;
                case "btnClose":
                    Close();
                    break;
                default:
                    break;
            }
        }
        YAHOO.util.Event.onContentReady("toolbar", function () {
            btnSave = new YAHOO.widget.Button("btnSave");
            btnSave.on("click", onButtonClick);
            btnClose = new YAHOO.widget.Button("btnClose");
            btnClose.on("click", onButtonClick);
        });
    } ();
    //查询按钮事件
    function SearchName() {
        var vName = $("#txtSearch").val();
        if (vName != "") {
            var url = "../../Extend/AjaxData/CustomOperating.aspx?action=check&entity=V_DatEmployeeForName&id=" + encodeURI(vName);
            YAHOO.util.Connect.asyncRequest('POST', url, {
                success: function (res) {
                    var inventory;
                    try {
                        inventory = res.responseText;
                        var obj = eval(inventory);
                        $("#divUser").html("");
                        var html = "";
                        $.each(obj, function (i) {
                            html += "<span><input id='" + obj[i].UserID + "' type='checkbox' class='cbSelected' />" +
                                     "<a href='javascript:void(0)' class='aSelectUser'>" + obj[i].Name + "</a>(" + obj[i].DeptName + ":" + obj[i].EmpCode + ")</span><br />";
                        });
                        $("#divUser").html(html);
                        bindSelectChange();
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        } else {
            $("#txtSearch").focus();
        }
    }
    //选择部门
    function ChangeDept() {
        var deptId = $('#SelectForDept').val();
        if (deptId == "" || deptId == null) {
            alert("请选择部门!");
        } else {
            var deptIndex = $("#SelectForDept").get(0).selectedIndex;
            if (deptIndex == 0) {
                return;
            } else {
                LoadDatas(deptId);
            }
        }
    }
    function LoadDatas(deptId) {
        var url = "../../Extend/AjaxData/CustomOperating.aspx?action=check&entity=V_DatEmployeeForUser&id=" + deptId;
        YAHOO.util.Connect.asyncRequest('POST', url, {
            success: function (res) {
                var inventory;
                try {
                    inventory = res.responseText;
                    var obj = eval(inventory);
                    $("#divUser").html("");
                    var html = "";
                    $.each(obj, function (i) {
                        html += "<span><input id='" + obj[i].UserID + "' type='checkbox' class='cbSelected' />" +
                                     "<a href='javascript:void(0)' class='aSelectUser'>" + obj[i].Name + "</a>(" + obj[i].DeptName + ":" + obj[i].EmpCode + ")</span><br />";
                    });
                    $("#divUser").html(html);
                    bindSelectChange();
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    };

    var bindSelectChange = function () {
        $(".aSelectUser,.cbSelected").unbind().bind("click", function () {
            var selectObj = $(this);
            if (selectObj.hasClass("aSelectUser"))
                selectObj = $(this).prev();

            if (selectObj.attr("checked")) {
                selectObj.attr("checked", false);
                var childNodes = tree2.getNodeCount();
                var topNodes = tree2.getNodesByProperty("href");
                $.each(topNodes, function (i) {
                    topNodes[i].highlightState = 0;
                });
                tree2.render();
            }
            else {
                selectObj.attr("checked", true);
                var userId = selectObj.attr("id");
                SetTreeNodeSelect(userId);
            }
        });
    };

    var Save = function () {
        var UserId = [], ProductLayoutId = [];
        $(".aSelectUser").each(function () {
            var obj = $(this).prev();
            if (obj.attr("checked")) {
                UserId.push(obj.attr("id"));
            }
        });
        var hiLit = tree2.getNodesByProperty('highlightState', 1);
        if (YAHOO.lang.isNull(hiLit)) {
            //ProductLayoutId.push("null");
        } else {
            for (var i = 0; i < hiLit.length; i++) {
                var node = tree2.getTreeDefinition();
                $("#testDIV").html("");
                $("#testDIV").html(hiLit[i].html);
                if ($("#testDIV").find("a").attr("href") != undefined) {
                    var id = $("#testDIV").find("a").attr("href").split("#")[1];
                    ProductLayoutId.push(id);
                }
            }
        }
        if (UserId.length == 0) {
            alert("请选择人员!");
            return;
        }
        if (ProductLayoutId.length == 0) {
            alert("请选择节点树!");
            return;
        }
        var instance = [{ "UserId": "", "ProductLayoutId": ""}];
        instance[0].UserId = UserId;
        instance[0].ProductLayoutId = ProductLayoutId;
        var json = $.toJSON(instance[0]).replace(/'/g, "[B]");
        $("#jsonDataContent").val(json);
        if (confirm("您确定保存吗?")) {
            trySave();
        }
    };
    var trySave = function () {
        var formObject = document.getElementById('form2');
        YAHOO.util.Connect.setForm(formObject);
        var cObj = YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/CustomOperating.aspx?flag=1&action=update&entity=CrmProductLayoutRole", callback);
    };
    var responseSuccess = function (res) {
        try {
            var str = res.responseText;
            if (str == "1") {
                alert("设置成功!");
                return;
            }
            else {
                alert(str);
            }
        } catch (e) {
        }
        finally {
        }
    };
    var responseFailure = function (error) {
        alert("数据错误：" + error.statusText);
    };
    var callback = {
        success: responseSuccess,
        failure: responseFailure
    };

    //SetValue
    var SetTreeNodeSelect = function (UserId) {
        var request = "../../Extend/AjaxData/CustomOperating.aspx?action=check&entity=CrmProductLayout&uid=" + UserId + "";
        YAHOO.util.Connect.asyncRequest('POST', request, {
            success: function (res) {
                var inventory;
                try {
                    var roleIDs = [];
                    var str = res.responseText;
                    var items = eval("[" + str + "]");
                    for (var i = 0; i < items.length; i++) {
                        roleIDs.push(items[i].ProductLayoutId);
                    }
                    var childNodes = tree2.getNodeCount();
                    var topNodes = tree2.getNodesByProperty("href");
                    $.each(topNodes, function (i) {
                        var currentObj = topNodes[i].html;
                        if (currentObj != null && currentObj != undefined) {
                            $("#testDIV").html("");
                            $("#testDIV").html(currentObj);
                            var id = $("#testDIV").find("a").attr("href").split("#")[1];
                            if (roleIDs.join(",").indexOf(id) > -1) {
                                topNodes[i].highlightState = 1;
                            }
                        }
                    });

                    /*var topNodes = tree2.getNodesByProperty("href");
                    var v = tree2._nodes[5].html;
                    for (var i = 0; i < childNodes; ++i) {
                        if (topNodes[i].href != "" || topNodes[i].href != null) {
                            var id = topNodes[i].href.split("#")[1];
                            if (roleIDs.join(",").indexOf(id) > -1) {
                                topNodes[i].highlightState = 1;
                            }
                        }
                    }*/

                    tree2.render();
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("读取菜单数据错误：" + error.statusText);
            }
        }, null);
    }
</script>
</html>
